﻿/*
 * Copyright (c) 2008 SQLI http://www.sqli.com/
 * Author: Olivier CARPENTIER
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Text;
using System.ComponentModel.Design;
using System.Reflection;
using System.Xml.Serialization;

namespace SQLI.WSS.WebControls.DataSource
{
    /// <summary>
    /// Collection of fields.
    /// </summary>
    [XmlRoot("Fields")]
    public class FieldsCollection : List<Field>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldsCollection"/> class.
        /// </summary>
        public FieldsCollection()
        {
        }
    }

    /// <summary>
    /// Field selected to be wrapped by the data source on a WSS List.
    /// </summary>
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Field
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Field"/> class.
        /// </summary>
        public Field()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Field"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public Field(string name)
        {
            this.Name = name;
        }

        private string name = string.Empty;
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        [Category("Behavior"),
        DefaultValue(""),
        Description("Field name"),
        NotifyParentProperty(true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    /// <summary>
    /// Editor class for fields type in Properties Panel of Visual Studio.
    /// </summary>
    public class FieldsCollectionEditor : CollectionEditor
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldsCollectionEditor"/> class.
        /// </summary>
        /// <param name="type">The type of the collection for this editor to edit.</param>
        public FieldsCollectionEditor(Type type)
            : base(type)
        {
        }

        /// <summary>
        /// Indicates whether multiple collection items can be selected at once.
        /// </summary>
        /// <returns>
        /// true if it multiple collection members can be selected at the same time; otherwise, false. By default, this returns true.
        /// </returns>
        protected override bool CanSelectMultipleInstances()
        {
            return false;
        }

        /// <summary>
        /// Gets the data type that this collection contains.
        /// </summary>
        /// <returns>
        /// The data type of the items in the collection, or an <see cref="T:System.Object"/> if no Item property can be located on the collection.
        /// </returns>
        protected override Type CreateCollectionItemType()
        {
            return typeof(Field);
        }
    }
}
